<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <!--{include file='comm/header.tpl'}-->
    <title>配送站列表</title>
    <!--{include file='comm/icheck.tpl'}-->
    <!--{include file='comm/comm_editor.tpl'}-->
</head>
<body>
<div class="container">

    
        <div class="comm_content">
            <div class="search-box">
                <div style="position:relative">
                    <form class="form-inline" id="search_form" onsubmit="return false;">
                    <span>
                       配送站名称：<input type="text"  class="form-control input-sm" name="distribution_name" id="distribution_name" style="width:150px;"/>
                    </span>
                    <span>
                       配送站编号：<input type="text"  class="form-control input-sm" name="distribution_code" id="distribution_code" style="width:150px;"/>
                    </span>
                    <span>
                        <button type="button" class="btn btn-primary" id="searchBtn" onclick="distribution.search();">查询</button>
                    </span>
                    <span>
                        <button type="button" class="btn btn-warning" id="clearBtn" onclick="distribution.clearUp();" >重置</button>
                    </span>
                    </form>
                </div>
            </div>
            <table id="flex" style="display:none"></table>
        </div>


</div>

<div id="pop" style="display: none;">
    <form class="form-horizontal" id="addForm" name="addForm" onsubmit="return false;">

        <div id="intervalTime">
            <input type="hidden" id="distribution_id" name="distribution_id" value=""/>
            <button class="btn btn-default" onclick="distribution.sameOrder();" type="button">添加运能设置</button>
            <table class="table table-bordered table-hover">
                <thead>
                <tr>
                    <th width="16%" style="text-align:center;">操作</th>
                    <th width="21%" style="text-align:center;">配送开始时间</th>
                    <th width="21%" style="text-align:center;">配送结束时间</th>
                    <th width="21%" style="text-align:center;">运单上限</th>
                    <th width="21%" style="text-align:center;">状态</th>
                </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </form>
</div>

<script type="text/javascript">

$("#flex").flexigrid({
    url:"/invokingAjax-backend/system/distributionSetting.ajax?operFlg=1",
    dataType: "json",
    colModel : [
        {display: "操作", width : 120, align: 'center'},
        {display: "配送站ID", name : "distribution_id" , width : 80 , sortable : true },
        {display: "配送站名称", width : 150},
        {display: "配送站编号", width : 100},
        {display: "配送站负责人", width : 100},
        {display: "联系手机", width : 90 , align:"left"},
        {display: "联系电话", width : 90 , align:"left"},
        {display: "配送站地址", width :200 , align:"left"},
        {display: "使用状态", width :90 , align:"left"}
    ],

    buttons : [
        {name: "添加", bclass: "add", onpress : "distribution.add.open"},
        {separator: true},
        {name: "删除", bclass: "delete", onpress : "distribution.del.check"},
        {separator: true},
        {name: "全选", bclass: "checkAll", onpress : "E.checkAllList"},
        {separator: true},
        {name: "取消全选", bclass: "cancelCheckAll", onpress : "E.cancelCheckAllList"}
    ],
    sortname: "distribution_id",
    sortorder: "asc",
    usepager: true,
    rp: 10,
    useRp: true,
    showTableToggleBtn: true,
    onSubmit: function() {
        var dt = $('#search_form').serializeArray();
        $("#flex").flexOptions({params: dt});
        return true;
    }
});

//定义一个配送对象
var distribution = {


    //搜索
    search: function() {
        var dt = $('#search_form').serializeArray();
        $("#flex").flexOptions({params: dt}).flexReload();
    },
    //添加配送站
    add:{
        open:function(){
            E.open("/quanliadmin/app/system/distributionSetting.pscript?operFlg=2", "" , {height: 500,width:880, top: 50, left: 250});
        }
    },

    //编辑配送站
    edit:{
        open:function( distribution_id ){
            E.open("/quanliadmin/app/system/distributionSetting.pscript?operFlg=2&distribution_id=" + distribution_id, "" , {height: 500,width:880, top: 50, left: 250});
        }
    },

    change: {

        check: function( distribution_id, flg ) {

            this.distribution_id = distribution_id;
            this.flg = flg;
            if (flg) {
                E.confirm('您确认禁用该配送站吗？', 'distribution.change.execute');
            } else {
                E.confirm('您确认启用该配送站吗？', 'distribution.change.execute');
            }

        },

        execute: function() {

            E.ajax_get({
                url: '/invokingAjax-backend/system/distributionSetting.ajax?operFlg=100&act=change',
                data: {
                    distribution_id: this.distribution_id,
                    flg: this.flg
                },
                call: 'distribution.change.result'
            });

        },

        result: function( o ) {

            if (o.code == 200) {
                E.alert(o.message, 2, 'distribution.load');
            } else {
                E.alert(o.message);
            }

        }

    },

    del: {

        p_array: '',

        check: function() {

            var p_obj = $('#flex').find('tr.trSelected');
            if (p_obj.length == 0) {
                E.alert('请选择要删除的配送站');
            } else {

                var temp_array = new Array();
                var error_flg = 0;

                $.each(p_obj, function(k, v) {
                    var id = $(v).attr('id').replace('row', '');
                    if (id == 1 || id == 2 || id == 3) {
                        error_flg = 1;
                        return false;
                    }
                    temp_array.push($(v).attr('id').replace('row', ''));
                });

                if (error_flg == 1) {
                    E.alert('ID[1,2,3]是系统默认的配送站<br />不可以删除');
                } else {
                    this.p_array = temp_array;
                    E.confirm('您确认要删除选中的配送站吗？', 'distribution.del.execute');
                }

            }

        },

        execute: function() {
            E.loadding.open('正在删除，请稍候...');
            E.ajax_post({
                url: '/invokingAjax-backend/system/distributionSetting.ajax?operFlg=100&act=del',
                data: {
                    p_array: this.p_array
                },
                call: 'distribution.del.result'
            });
        },

        result: function( o ) {

            E.loadding.close();
            if (o.code == 200) {
                E.alert('删除成功', 2, 'distribution.load');
            } else {
                E.alert(o.message);
            }

        }

    },

    clearUp:function(){
        $("#distribution_name").val('');
        $("#distribution_code").val('');
        var dt = $('#search_form').serializeArray();
        $("#flex").flexOptions({params: dt}).flexReload();
    },

    load: function() {
        $("#flex").flexReload();
    },

    //运能设置
    editSku: function( distribution_id ){

        $("#distribution_id").val(distribution_id);

        E.popup.open({
            id:"pop",
            title: '编辑运能设置',
            css: "width: 800px;",
            btnName: "保存",
            btnCall:"distribution.check();"
        });

        $('.square-radio').iCheck({
            checkboxClass: 'icheckbox_square-blue',
            radioClass: 'iradio_square-blue',
            increaseArea: '20%'
        });

        if ( distribution_id ) {
            E.loadding.open();
            E.ajax_post({
                url : '/invokingAjax-backend/system/distributionSetting.ajax?operFlg=4',
                data: {
                    distribution_id:distribution_id
                },
                call: 'distribution.skuCall'
            });

        }
    },

    skuCall: function (o) {

        E.loadding.close();
        if (o.code == 200) {
            var sameStr = '';



            $.each(o.data.space, function(k, v){

                sameStr += '<tr id=same_' + distribution.sameCount+ '>';
                sameStr += '<td style="text-align:center;">';
                sameStr += '<a href="javascript:void(0);" onclick="distribution.sameDel(' + distribution.sameCount+ ');">删除</a>';
                sameStr += '</td>';
                sameStr += '<td>';
                sameStr += '<input id=send_start_time_' + distribution.sameCount+ ' name=send_start_time_' + distribution.sameCount+ ' class="form-control" type="text" style="width:118px;" value=' + v.send_start_time + ' maxlength="20">';
                sameStr += '</td>';
                sameStr += '<td>';
                sameStr += '<input id=send_end_time_' + distribution.sameCount+ ' name=send_end_time_' + distribution.sameCount+ ' class="form-control" type="text" style="width:118px;" value=' + v.send_end_time + ' maxlength="20">';
                sameStr += '</td>';
                sameStr += '<td>';
                sameStr += '<input id=max_bill_' + distribution.sameCount+ ' name=max_bill_' + distribution.sameCount+ ' class="form-control" type="text" style="width:118px;" value=' + v.max_bill + ' maxlength="20">';
                sameStr += '</td>';
                sameStr += '<td>';
                sameStr +=  '<div class="fl" style="margin-left:10px;" >';
                if(v.send_status == 0){
                    sameStr += '<input type="radio" value="0" name=send_status_' + distribution.sameCount + ' class="square-radio" checked>';
                }else{
                    sameStr += '<input type="radio" value="0" name=send_status_' + distribution.sameCount + ' class="square-radio" >';
                }
                sameStr += '<label for="send_status_' + distribution.sameCount + '">&nbsp;不配送</label>';
                sameStr += '</div>';
                sameStr += '<div class="fl" style="margin-left:10px;" >';
                if(v.send_status == 1){
                    sameStr += '<input type="radio" value="1" name=send_status_' + distribution.sameCount +  ' class="square-radio" checked>';
                }else{
                    sameStr += '<input type="radio" value="1" name=send_status_' + distribution.sameCount + ' class="square-radio">';
                }
                sameStr += '<label for="send_status_' + distribution.sameCount + '">&nbsp;可配送</label>';
                sameStr += '</div>';
                sameStr += '</td>';
                sameStr += '</tr>';

                distribution.sameCount++;


            });

            $('#intervalTime table tbody').append(sameStr);

        }
    },


    sameCount: 1,

    sameOrder: function(){
        var sameStr = '';
        sameStr += '<tr id=same_' + distribution.sameCount + '>';
        sameStr += '<td style="text-align:center;">';
        sameStr += '<a href="javascript:void(0);" onclick="distribution.sameDel(' + distribution.sameCount + ');">删除</a>';
        sameStr += '</td>';
        sameStr += '<td>';
        sameStr += '<input id=send_start_time_' + distribution.sameCount + ' name=send_start_time_' + distribution.sameCount + ' class="form-control" type="text" style="width:118px;" value="" maxlength="20">';
        sameStr += '</td>';
        sameStr += '<td>';
        sameStr += '<input id=send_end_time_' + distribution.sameCount + ' name=send_end_time_' + distribution.sameCount + ' class="form-control" type="text" style="width:118px;" value="" maxlength="20">';
        sameStr += '</td>';
        sameStr += '<td>';
        sameStr += '<input id=max_bill_' + distribution.sameCount + ' name=max_bill_' + distribution.sameCount + ' class="form-control" type="text" style="width:118px;" value="" maxlength="20">';
        sameStr += '</td>';
        sameStr += '<td>';
        sameStr +=  '<div class="fl" style="margin-left:10px;" >';
        sameStr += '<input type="radio" value="0" class="square-radio" name=send_status_' + distribution.sameCount + ' >';
        sameStr += '<label for="send_status_">&nbsp;不配送</label>';
        sameStr += '</div>';
        sameStr += '<div class="fl" style="margin-left:10px;" >';
        sameStr += '<input type="radio" value="1" class="square-radio" checked name=send_status_' + distribution.sameCount + '   >';
        sameStr += '<label for="send_status_">&nbsp;可配送</label>';
        sameStr += '</div>';
        sameStr += '</td>';
        sameStr += '</tr>';

        this.sameCount++;

        $('#intervalTime table tbody').append(sameStr);
    },

    sameDel: function( distribution_id ){
        $('#same_' + distribution_id).remove();
    },


    //保存检查
    check: function(){

        var error_msg = '';

        this.dt = E.getFormValues('addForm');

        //配送间隔
        var eachMsg = '';
        $('[name*=send_start_time]').each(function(k, v){
            var send_start_time = E.trim($(this).val());
            if (send_start_time == '' || !E.isDate(send_start_time)) {
                eachMsg = '请填写正确的运能设置格式';
            }
        });
        if (eachMsg != '') {
            E.alert(eachMsg);
            return false;
        }
        $('[name*=send_end_time]').each(function(k, v){
            var send_end_time = E.trim($(this).val());
            if (send_end_time == '' || !E.isDate(send_end_time)) {
                eachMsg = '请填写正确的运能设置格式';
            }
        });

        $('[name*=max_bill]').each(function(k, v){
            var max_bill = E.trim($(this).val());
            if (max_bill == '' || !E.isDigital(max_bill)) {
                eachMsg = '运单上限必须为整数';
            }
        });

        if (eachMsg != '') {
            E.alert(eachMsg);
            return false;
        }


        this.dt.sameCount = this.sameCount;

        if (error_msg != '') {
            E.alert(error_msg);
        } else {
            E.confirm('您确认添加运能设置吗？', 'distribution.save');
        }

    },

    //ajax保存
    save: function(){

        E.ajax_post({
            url : '/invokingAjax-backend/system/distributionSetting.ajax?operFlg=5',
            data: {
                params: distribution.dt
            },
            call: 'distribution.result'
        });
    },

    result: function( o ) {

        E.loadding.close();
        //E.popup.close();
        if (o.code == 200) {
            E.alert('保存成功', 2, 'distribution.load');
        } else {
            E.alert(o.message);
        }

    }


};

</script>

</body>
</html>